package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Charge;
import cn.wolfcode.crm.domain.Paymen;
import cn.wolfcode.crm.query.ChargeQueryObject;
import cn.wolfcode.crm.query.PaymenQueryObject;
import cn.wolfcode.crm.query.TaskQueryObject;
import cn.wolfcode.crm.service.IChargeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2018/6/12.
 */

@Controller
@RequestMapping("charge")
public class ChargeController {

    @Autowired
    private IChargeService chargeService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"charge:view", "考勤页面"}, logical = Logical.OR)
    public String view() {
        return "charge";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"charge:query", "考勤列表"}, logical = Logical.OR)
    public PageResult query(ChargeQueryObject qo) {
        return chargeService.query(qo);
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"charge:delete","角色删除"},logical = Logical.OR)
    public JsonResult delete(Long id) {
        try {
            chargeService.deleteById(id);
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response, TaskQueryObject qo) throws IOException {
        response.setHeader("Content-disposition", "attachment;filename=task.xls");
        //创建excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("sheet01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("保单号");
        row.createCell(1).setCellValue("投保人");
        row.createCell(2).setCellValue("电话");
        row.createCell(3).setCellValue("客户地址");
        row.createCell(4).setCellValue("身份证");
        row.createCell(5).setCellValue("核保单号");
        row.createCell(6).setCellValue("保费金额");
        row.createCell(7).setCellValue("缴费状态");
        row.createCell(8).setCellValue("缴费方式");
        row.createCell(9).setCellValue("起保日期");
        row.createCell(10).setCellValue("止保日期");
        row.createCell(11).setCellValue("保险机构");
        row.createCell(12).setCellValue("录入人");
        List<Charge> list = chargeService.selectAll();
        for (int i = 0; i < list.size(); i++) {
            Charge charge = list.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(charge.getChargeSn());
            row.createCell(1).setCellValue(charge.getClient().getName());
            row.createCell(2).setCellValue(charge.getClient().getTel());
            row.createCell(3).setCellValue(charge.getClient().getAddress());
            row.createCell(4).setCellValue(charge.getClient().getIdNo());
            row.createCell(5).setCellValue(charge.getSn());
            row.createCell(6).setCellValue(charge.getMoney().toString());
            row.createCell(7).setCellValue(charge.getStatus()? "缴费完成":"缴费完成");
            if (charge.getWay()==1){
                row.createCell(8).setCellValue("现金支付");
            }else if(charge.getWay()==2){
                row.createCell(8).setCellValue("刷卡支付");
            }else {
                row.createCell(8).setCellValue("支票支付");
            }
            CreationHelper createHelper = workbook.getCreationHelper();
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(
                    createHelper.createDataFormat().getFormat("m/d/yy"));
            HSSFCell cellDate = row.createCell(9);
            cellDate.setCellStyle(cellStyle);
            cellDate.setCellValue(charge.getStartDate());

            createHelper = workbook.getCreationHelper();
            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(
                    createHelper.createDataFormat().getFormat("m/d/yy"));
            cellDate = row.createCell(10);
            cellDate.setCellStyle(cellStyle);
            cellDate.setCellValue(charge.getEndDate());

            row.createCell(11).setCellValue(charge.getOrganization().getName());
            row.createCell(12).setCellValue(charge.getEmployee().getUsername());
        }
        workbook.write(response.getOutputStream());
    }
}